
//var id = location.search.urlToObj().id;
// var reg = new RegExp("(^|&)" + "id" + "=([^&]*)(&|$)");
// var r = window.location.search.substr(1).match(reg);

var Nid="";
(function ($) {
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
})(jQuery);
var Nid=$.getUrlParam('id');
readercourse_dateils()//获取课程详细信息列表
table()//table切换
function readercourse_dateils(){
  $.ajax({
    url:"/api/product/"+Nid,
    method:"get",
    success:function(data){
    //  console.log(data);
    title(data.data);
    courselist(data.data);
    the_instructor(data.data);
    orderHandle(data.data);

    imgLazyload()
    }
  })


}
//获取title
function title(item){
  var $courseDetail=$('#j-course-detail').html("");
  var $dl = $('<div class="cover uk-float-left">\
    <img class="lazy" data-original="'+ item.cover +'" src="/home/img/lazyload_course.png" alt="">\
  </div>\
  <div class="content">\
    <h2>'+ item.title +'</h2>\
    <p class="uk-margin-small">'+i18next.t('home.speaker')+'：<span>'+ (item.masterinfo ? item.masterinfo.lastname : '') +'</span></p>\
    <p class="uk-margin-small"><span>'+i18next.t('home.duration')+'：'+ item.duration +''+i18next.t('home.minute')+'</span></p>\
    <p class="price">'+ (item.price === '0.00'?i18next.t('home.free'):('￥'+item.price)) + '</p>\
    <button class="uk-button uk-button-primary uk-button-large" id="js-buy" data-uk-modal="{center:true}">'+i18next.t('home.buynow')+'</button>\
  </div>').appendTo($courseDetail);
//  <p class="intro" style="display:none">'+ item.intro +'</p>\
  $dl.find('.uk-button').on('click', function () {

  })

 }
// //table 切换
 function table(){
   $('#j-tab-buttons > span').on('click', function () {
     $(this).addClass('active')
       .siblings().removeClass('active')
       .parent().next().children().eq($(this).index()).addClass('active')
       .siblings().removeClass('active')
   })
 }
//获取课程列表的数据
function courselist(data){
  var $ul=$("#j-lessons").html("");
  if(data.intro==""){
    var $course_overview=$('#course_overview');
    $('<dl class="nondata" style="text-align:center">\
      <dt><img src="../home/img/defaule-03.png" style="width:200px;height:200px;margin-top:100px;"></dt>\
      <dd>'+i18next.t('home.nodata')+'</dd>\
    </dl>').appendTo($course_overview);
  }else{
    $('#course_overview').html(data.intro);
  }
  if(data.lessons.length==0){
    $('<dl class="nondata" style="text-align:center">\
      <dt><img src="../home/img/defaule-03.png" style="width:200px;height:200px;margin-top:100px;"></dt>\
      <dd>'+i18next.t('home.nodata')+'</dd>\
    </dl>').appendTo($('#classhouer'));
  }else{
    $.each(data.lessons,function(index,item){
      var $lesso_li=$('<li>\
        '+i18next.t('home.classhourr')+''+ (index+1) +'：<span>'+ item.title +'</span>\
        <span class="uk-float-right">'+i18next.t('home.classhours')+'：'+ item.schedules +'</span>\
        </li>').appendTo($ul)
        if(item.hasvideouri==1){
        $('<a href="'+item.videouri+'" target="_blank" style="width:16px;height:13px;display:inline-block;margin:0px 10px;float:right"><img style="width:16px;height:13px;" src="../home/img/audition.png" titl="试听"></a>').appendTo($lesso_li);
      }
      if(item.hasliveuri==1){
        $('<a href="'+item.liveuri+'" target="_blank" style="width:16px;height:13px;display:inline-block;margin:0px 10px;float:right"><img style="width:16px;height:13px;" src="../home/img/live.png" titl="直播"></a>').appendTo($lesso_li);
      }
    })
  }
}
function course_overview(data){
    var $course_overview=$('#course_overview');

}
//授课老师
function the_instructor(item){
  if(item.masterinfo == null) {
    return
  }

  $('<div class="little-cover uk-cover uk-border-circle uk-float-left">\
    <img class="avatar-md" src="'+ item.masterinfo.photo +'" alt="" class="">\
  </div>\
  <div>\
    <p class="uk-h3">'+ (item.masterinfo ? item.masterinfo.lastname : '') +'</p>\
    <p>'+ (item.masterinfo ? item.masterinfo.introduce : '') +'</p>\
  </div>').appendTo($('#j-teacher-info'))

}

function orderHandle(productInfo) {
  var modal = UIkit.modal('#modal1')
  var $phone = $('[name=phone]')
  var $student = $('[name=student]')

  $('#modal1').on({
    'show.uk.modal': function(){
      $phone.val('')
      $student.val('')
    }
  });

  if(hasLogin && $.urlToObj(location.href).modal === 'open') {
    modal.show()
  }


  $('#js-buy').on('click', function () {
    if(hasLogin) {
      modal.show()
    } else {
      location.assign('login.html?courseid='+productInfo.id)
    }
  })

  $('#js-submit').on('click', function () {

    var phone = $phone.val()
    var student = $student.val()

    $phone.next().hide()
    $student.next().hide()

    if(!phone) {
      $phone.next().show().text(i18next.t('home.pleasephone'))//请输入手机号码
      return
    }

    if(!/^1\d{10}$/.test(phone)) {
      $phone.next().show().text(i18next.t('home.phoneformat'))//请输入正确的手机号码格式
      return
    }

    if(!student) {
      $student.next().show().text(i18next.t('home.studentname'))//请输入学生真实姓名
      return
    }

    $.ajax({
      url: '/api/user/current'
    }).done(function (res) {
      if(res.status.code !== 0) {
        return
    }

      $.ajax({
        url: '/api/student/user/' + res.data.userid
      }).done(function (res) {
        if(res.status.code !== 0) {
          return
        }

        $.ajax({
          url: '/api/order',
          method: 'post',
          data: {
            type: productInfo.type,
            studentid: res.data.studentuid,
            mobile: phone,
            studentname: student,
            productid: productInfo.id,
            productname: productInfo.title,
            orderdate: $.getTimeStamp(),
            description: productInfo.intro,
            amount: productInfo.price
          },
          success: function (res) {
            modal.hide()
            if(res.status.code == 0) {
              location.assign('/student/index.html#student_order')
            }
          }
        })
      })
    })



  })
}
